package socket;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

public class MyVerticleHttp extends AbstractVerticle {

	@Override
	public void start() throws Exception {
		WebClientOptions options = new WebClientOptions();
		options.setDefaultHost("localhost");
		options.setDefaultPort(8088);
		WebClient client = WebClient.create(vertx, options);
		client.get("/test/get/simple").send( r ->{
			if(r.succeeded()){
				JsonObject jsonObject = r.result().bodyAsJsonObject();
				System.out.println(jsonObject);
			}
		});


		client.get("/test/get2").addQueryParam("name","www").send( r ->{
			if(r.succeeded()){
				JsonObject jsonObject = r.result().bodyAsJsonObject();
				System.out.println(jsonObject);
			}
		});

		client.post("/test/param").addQueryParam("username","www").send( r ->{
			if(r.succeeded()){
				JsonObject jsonObject = r.result().bodyAsJsonObject();
				System.out.println(jsonObject);
			}
		});

		MultiMap form = MultiMap.caseInsensitiveMultiMap();
		form.set("username", "Dale");
		form.set("lastName", "Cooper");
		client.post("/test/param").sendForm( form,r ->{
			if(r.succeeded()){
				JsonObject jsonObject = r.result().bodyAsJsonObject();
				System.out.println(jsonObject);
			}
		});

		User user = new User();
		user.setId(111L);
		user.setRealName("wwwwwwwwwwwwwwwwwww");
		client.post("/test/body").sendJson( user, r ->{
			if(r.succeeded()){
				JsonObject jsonObject = r.result().bodyAsJsonObject();
				System.out.println(jsonObject);
			}
		});

	}

	public static void main(String[] args) {
		Vertx vertx = Vertx.vertx();
		vertx.deployVerticle(new MyVerticleHttp());
	}
}
